<template>
  <div class="auth-container" style="position: fixed;">
    <div v-if="isLogin">
      <h2>登录</h2>
      <div class="input-container">
        <input v-model="email" type="email" placeholder="用户名/邮箱" />
      </div>
      <div class="input-container">
        <input v-model="password" type="password" placeholder="密码" />
      </div>
      <div class="input-container">
        <button @click="btnLogin">登录</button>
      </div>
    </div>

    <div v-else>
      <h2>注册</h2>
      <div class="input-container">
        <input v-model="username" placeholder="用户名/邮箱" />
      </div>
      <div class="input-container">
        <input v-model="password" type="password" placeholder="密码" />
      </div>
      <div class="input-container">
        <input v-model="confirmPassword" type="password" placeholder="确认密码" />
      </div>
      <div class="input-container">
        <input v-model="realName" placeholder="姓名" />
      </div>
      <div class="input-container">
        <input v-model="stuNum" type="number" placeholder="学号" />
      </div>
      <div class="input-container">
        <el-select v-model="className" placeholder="选择班级" size="large">
          <el-option :key="1" label="23软件技术1班" value="23软件技术1班" />
          <el-option :key="2" label="23软件技术2班" value="23软件技术2班" />
        </el-select>
      </div>
      <div class="input-container">
        <button @click="register">注册</button>
      </div>
    </div>

    <div class="toggle-buttons" style="margin-top: 20px;">
      <button :class="{ active: isLogin }" @click="isLogin = true">登录</button>
      <button :class="{ active: !isLogin }" @click="isLogin = false">注册</button>
    </div>

    <div class="home-link">
      <RouterLink to="/">返回首页</RouterLink>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '../stores/user';
import { login, reg } from '../utils/request';

const userStore = useUserStore();
const router = useRouter();

// 控制显示登录还是注册表单
const isLogin = ref(true);

// 初始化账号和密码
const email = ref('23002979');
const password = ref('123456');

const username = ref('');
const confirmPassword = ref('');
const realName = ref('');
const stuNum = ref('');
const className = ref('23软件技术1班');

const btnLogin = async () => {
  const r = await login({
    username: email.value,
    pwd: password.value,
  });
  if (r) {
    sessionStorage.setItem('user', JSON.stringify(r)); // 存储用户信息到 sessionStorage
    userStore.setUser(r);
    router.push('/movie');
  } else {
    alert('账号或密码错误');
  }
};

const register = async () => {
  // 注册逻辑
  if (password.value !== confirmPassword.value) {
    alert('两次输入的密码不一致');
    return;
  }
  const r = await reg({
    username: username.value,
    pwd: password.value,
    realName: realName.value,
    stuNum: stuNum.value,
    className: className.value,
  });
  console.log(r);
  if (r.errno) return alert(r.message);
  // 修改跳转路径为 /loginregister
  router.push('/loginregister');
  // 显示登录表单
  isLogin.value = true;
};

// 监听 isLogin 的变化
watch(isLogin, (newValue) => {
  if (!newValue) {
    // 切换到注册表单时清空所有输入框
    username.value = '';
    password.value = '';
    confirmPassword.value = '';
    realName.value = '';
    stuNum.value = '';
    className.value = '23软件技术1班';
  }
});
</script>

<style scoped>
.auth-container {
  width: 30%;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;
  text-align: center;
  background-color: #f0f0f0;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.toggle-buttons {
  display: flex;
  justify-content: center;
  margin-bottom: 1.5rem;
  gap: 10px;
}

.toggle-buttons button {
  padding: 8px 16px;
  border: none;
  background-color: #e0e0e0;
  color: #333;
  cursor: pointer;
  border-radius: 4px;
  font-weight: 500;
  transition: all 0.3s;
}

.toggle-buttons button.active {
  background-color: #28a745;
  color: white;
}

.input-container {
  margin-bottom: 1rem;
}

.input-container input,
.input-container el-select {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
}

.auth-link {
  display: block;
  margin-top: 1rem;
  color: #28a745;
  text-decoration: none;
  font-size: 0.9rem;
}

.auth-link:hover {
  text-decoration: underline;
}

.input-container button {
  width: 100%;
  padding: 10px 20px;
  background-color: #28a745;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.3s;
}

.input-container button:hover {
  background-color: #218838;
}

.home-link {
  margin-top: 1.5rem;
  font-size: 0.9rem;
}

.home-link a {
  color: #28a745;
  text-decoration: none;
  font-weight: bold;
}

.home-link a:hover {
  text-decoration: underline;
}
</style>